package gui.graphs3d;

import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import java.awt.Color;
import javax.media.j3d.Appearance;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.swing.JTextArea;
import javax.vecmath.Color3f;

/**
 *
 * @author Elad
 */
public class MousePickShapeBehavior extends PickMouseBehavior {

    private Appearance savedAppearance = null;
    private Shape3D oldShape = null;
    private Appearance highlightAppearance;
    private JTextArea output;

    public MousePickShapeBehavior(Canvas3D canvas,JTextArea output, BranchGroup root, Bounds bounds) {
        super(canvas, root, bounds);
        this.output=output;
        this.setSchedulingBounds(bounds);
        // root.addChild(this);
        Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f highlightColor = new Color3f(new Color(188,17,56));
        Material highlightMaterial = new Material(highlightColor, black,
                highlightColor, white,
                80.0f);
        highlightAppearance = new Appearance();
        highlightAppearance.setMaterial(highlightMaterial);
        pickCanvas.setMode(PickTool.BOUNDS);

    }

    @Override
    public void updateScene(int xpos, int ypos) {
        PickResult pickResult = null;
        Shape3D shape = null;
        pickCanvas.setShapeLocation(xpos, ypos);
        pickResult = pickCanvas.pickClosest();
        if (pickResult != null) {
            shape = (Shape3D) pickResult.getNode(PickResult.SHAPE3D);
        }

        if (oldShape != null) {
            oldShape.setAppearance(savedAppearance);
        }
        if (shape != null) {
            savedAppearance = shape.getAppearance();
            oldShape = shape;            
            output.setText( (String) shape.getUserData()+"\n");
            shape.setAppearance(highlightAppearance);
        }
    }
}
